package com.example.desksginktapp.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.desksginktapp.R
import com.example.httplibrary.beans.VisitorDataListResultBean

class VisitorAdapter(
    private val visitorList: MutableList<VisitorDataListResultBean.ResultDTO.RecordsDTO>
) : RecyclerView.Adapter<VisitorAdapter.VisitorViewHolder>() {

    // 定义操作回调接口
    interface OnItemClickListener {
        fun onItemClick(visitor: VisitorDataListResultBean.ResultDTO.RecordsDTO, position: Int)
    }

    private var itemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.itemClickListener = listener
    }

    class VisitorViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val nameText: TextView = itemView.findViewById(R.id.tv_name)
        val reasonText: TextView = itemView.findViewById(R.id.tv_reason)
        val visitTimeText: TextView = itemView.findViewById(R.id.tv_visit_time)
        val visitObjectText: TextView = itemView.findViewById(R.id.tv_visit_object)
        val visitorCardText: TextView = itemView.findViewById(R.id.tv_visitor_card)
        val signInStatusText: TextView = itemView.findViewById(R.id.tv_sign_in_status)
        val signOutStatusText: TextView = itemView.findViewById(R.id.tv_sign_out_status)
        val operateButton: Button = itemView.findViewById(R.id.btn_operate)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VisitorViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_visitor, parent, false)
        return VisitorViewHolder(view)
    }

    override fun onBindViewHolder(holder: VisitorViewHolder, position: Int) {
        val visitor = visitorList[position]

        holder.nameText.text = visitor.visitorName
        holder.reasonText.text = visitor.visitMatter
        holder.visitTimeText.text = visitor.timeTend
        holder.visitObjectText.text = visitor.interviewName
        holder.visitorCardText.text = visitor.phone
        holder.signInStatusText.text = visitor.visitStatus
        holder.signOutStatusText.text = visitor.visitStatus

        // 设置操作按钮点击事件
        holder.operateButton.setOnClickListener {
            itemClickListener?.onItemClick(visitor, position)
        }
    }

    override fun getItemCount(): Int = visitorList.size

    // 更新数据的方法
    fun updateData(newList: List<VisitorDataListResultBean.ResultDTO.RecordsDTO>) {
        visitorList.clear()
        visitorList.addAll(newList)
        notifyDataSetChanged()
    }

    // 添加单条数据
    fun addItem(visitor: VisitorDataListResultBean.ResultDTO.RecordsDTO) {
        visitorList.add(visitor)
        notifyItemInserted(visitorList.size - 1)
    }

    // 删除指定位置的数据
    fun removeItem(position: Int) {
        if (position >= 0 && position < visitorList.size) {
            visitorList.removeAt(position)
            notifyItemRemoved(position)
        }
    }
}
